package com.dst.note;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;

import com.dst.note.R;
import com.dst.note.lisenter.ClockListener;
import com.dst.note.route.Router;

public class ClockActivity extends BaseActivity {
    //闹铃
    private MediaPlayer mediaPlayer;
    //震动
    private Vibrator mVibrator = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_clock);
        initData();
        clock();
    }

    private void clock() {
        mediaPlayer.start();
        long[] pattern = new long[]{1500, 1000};
        mVibrator.vibrate(pattern, 0);


        String noteId = getIntent().getStringExtra(ClockService.EXTRA_EVENT_ID);
        final ClockDialogFragment fragment = ClockDialogFragment.newInstance(noteId);
        fragment.show(getSupportFragmentManager(), ClockDialogFragment.class.getSimpleName());
        fragment.setCancelListener(new ClockListener() {
            @Override
            public void confirm() {
                mediaPlayer.stop();
                mVibrator.cancel();
                String noteId = getIntent().getStringExtra(ClockService.EXTRA_EVENT_ID);
                Router.with(ClockActivity.this).route("editnote").intent("id", noteId).go();
                finish();
            }

            @Override
            public void cancel() {
                mediaPlayer.stop();
                mVibrator.cancel();
                finish();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        clock();
    }

    protected void initData() {
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.clock);
        mVibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        Intent intent = getIntent();
//        event = getIntent().getParcelableExtra(ClockService.EXTRA_EVENT);
//        if (event == null) {
//            finish();
//        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        clock();
    }

}
